package cn.colins.raft.enity;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description 日志实体
 * @Author czl
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/7/19
 */
public class LogEntity implements Serializable,Comparable {

    private long term;  // 任期

    private long index; // 索引

    private String command; // 命令

    public LogEntity(long term, long index, String command) {
        this.term = term;
        this.index = index;
        this.command = command;
    }


    @Override
    public int compareTo(Object o) {
        if (o == null) {
            return -1;
        }
        LogEntity logEntry = (LogEntity) o;
        if (this.getIndex() > logEntry.getIndex()) {
            return 1;
        }
        return -1;
    }

    public long getTerm() {
        return term;
    }

    public long getIndex() {
        return index;
    }


    public void setIndex(long index) {
        this.index = index;
    }

    public static void main(String[] args) {
        List<LogEntity> logEntities=new ArrayList<>();
        logEntities.add(new LogEntity(1,1,null));
        logEntities.add(new LogEntity(2,2,null));
        logEntities.add(new LogEntity(1,3,null));
        logEntities.add(new LogEntity(3,4,null));
        logEntities.add(new LogEntity(1,5,null));

        Collections.sort(logEntities);

        for(LogEntity logEntity:logEntities){
            System.out.println(JSONObject.toJSON(logEntity));
        }

    }

    public String getCommand() {
        return command;
    }
}
